package zy.blue7.flowable.service;

import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:flowable-context.xml")
public class HistoryServiceTest {
    ProcessEngine processEngine;

    RepositoryService repositoryService;
    HistoryService historyService;

    @Before
    public void testProcessEngine() {

        /**
         * 获取默认的流程引擎类
         */
        //todo 主要是通过flowable的风格的配置文件  创建一个 {@see org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration} 配置类
        //todo 该配置类 中创建了 多个service， 然后赋值给processengine
        processEngine = ProcessEngines.getDefaultProcessEngine();

        System.out.println("默认的流程引擎类 " + processEngine);

        //todo 获取的service

        DynamicBpmnService dynamicBpmnService = processEngine.getDynamicBpmnService();
        System.out.println(dynamicBpmnService);

        FormService formService = processEngine.getFormService();
        System.out.println(formService);

        historyService = processEngine.getHistoryService();
        System.out.println(historyService);

        IdentityService identityService = processEngine.getIdentityService();
        System.out.println(identityService);

        ManagementService managementService = processEngine.getManagementService();
        System.out.println(managementService);

        repositoryService = processEngine.getRepositoryService();
        System.out.println(repositoryService);

        RuntimeService runtimeService = processEngine.getRuntimeService();
        System.out.println(runtimeService);

        TaskService taskService = processEngine.getTaskService();
        System.out.println(taskService);

        ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
        System.out.println(processEngineConfiguration);

        String name = processEngine.getName();
        System.out.println("流程引擎的名字：" + name);

    }


    /**
     * todo 历史数据查询---
     *  历史流程实例数据查询
     *      历史流程实例 act_hi_procinst 表。历史流程实例与运行流程实例是一一对应的关系
     *
     *      注意是 流程实例，不是执行实例，是历史流程实例
     *
     *
     */

    /**
     * todo 历史数据查询
     *  使用的是HistoryService服务类
     *  HistoryService服务类查询的是 以 act_hi 开头的表
     *  HistoryService的实现类是 HistoryServiceImpl类
     *
     *
     *
     */


    /**
     * todo 查询历史流程实例 createHistoricProcessInstanceQuery
     */
    @Test
    public void createHistoricProcessInstanceQuery() {
        String processInstanceId = "10001";
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();


        System.out.println(historicProcessInstance.getId());
        System.out.println(historicProcessInstance.getName());
        System.out.println(historicProcessInstance.getProcessDefinitionId());
        System.out.println(historicProcessInstance.getProcessDefinitionKey());
        System.out.println(historicProcessInstance.getProcessDefinitionName());
        System.out.println(historicProcessInstance.getClass());
        System.out.println(historicProcessInstance.getStartUserId());
        System.out.println("开始节点" + historicProcessInstance.getStartActivityId());
        System.out.println("结束节点 " + historicProcessInstance.getEndActivityId());


    }

    /**
     * todo 历史活动信息查询
     * 历史活动表是act_hi_actinst.  活动节点数据都会存储在这个表中。
     * 注意这里的活动节点 包含已经完成的活动节点，还有一些正在准备完成的节点  ，
     */
    @Test
    public void createHistoricActivityInstanceQuery() {
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .list();

        for (HistoricActivityInstance historicActivityInstance : list) {
            System.out.println("====================");
            System.out.println(historicActivityInstance.getId());
            System.out.println(historicActivityInstance.getStartTime());
            System.out.println(historicActivityInstance.getEndTime());
            System.out.println(historicActivityInstance.getActivityId());
            System.out.println(historicActivityInstance.getActivityName());
            System.out.println(historicActivityInstance.getActivityType());
            System.out.println(historicActivityInstance.getAssignee());
            System.out.println(historicActivityInstance.getCalledProcessInstanceId());
            System.out.println(historicActivityInstance.getExecutionId());
        }
    }
}
